﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using thousandclear.Data;
using thousandClear.Areas.Admin.Models;
using thousandClear.Util.Setting;

namespace thousandClear.Areas.Admin.Controllers
{
    public class GoodController : BaseController
    {
        // GET: Admin/Good
        public ActionResult Index(int? page, int? count)
        {
            string level = "1";
            level=GetCookie("level");
            ViewBag.level = level;
            page = page ?? 1;
            count = count ?? 10;
            int totalCount = 0;
            int totalPage = 1;
            int currentPage = page.Value;
            int pageCount = count.Value;
            List<GoodModel> list = new List<GoodModel>();
            using (var db = new qianjingEntities())
            {
                decimal zero = 0;
                var query = db.O_Goods.AsQueryable().Where(a => a.RowStatus == 1);
                totalCount = query.Count();
                var records = query.OrderByDescending(a => a.OGSID).Skip((page.Value - 1) * count.Value).Take(count.Value).ToList();
          
                foreach (var item in records)
                {
                    list.Add(new GoodModel
                    {

                        OGSID = Convert.ToInt16(item.OGSID),
                        ogsSalePrice = item.ogsSalePrice,
                        ogsMarketPrice = item.ogsMarketPrice == null ? zero : item.ogsMarketPrice.Value,
                        ogsDesc = item.ogsDesc,
                        ogsSeqNum = item.ogsSeqNum == null ? 0 : item.ogsSeqNum.Value,
                        ogsTime = item.ogsTime,
                        ogsType = item.ogsType,
                        ogsTimeMonth=Convert.ToInt16(item.ogsTimeMonth),
                        ogsSignTime = item.ogsSignTime == null ? 0 : item.ogsSeqNum.Value,
                        CreatedDate = item.CreatedDate
                    });
                }

            }


            ViewBag.TotalCount = totalCount;
            totalPage = int.Parse((totalCount / count).ToString()) + (totalCount % count == 0 ? 0 : 1);
            ViewBag.TotalPage = totalPage;
            ViewBag.CurrentPage = currentPage;
            ViewBag.PageCount = pageCount;

            return View(list);
        }

        //HttpGet: Admin/Good
        [HttpGet]
        public GoodControllerResponse remove(long id)
        {
            GoodControllerResponse resp = new GoodControllerResponse();

            using (var db = new qianjingEntities())
            {

                var query = db.O_Goods.FirstOrDefault(a => a.OGSID == id);

                if (query == null)
                {
                    resp.ErrMsg = "该记录不存在";
                    return resp;
                }
                query.RowStatus = 2;
                query.UpdatedDate = DateTime.Now;

                db.SaveChanges();
            }
            return resp;


        }


        // GET: Good/Add
        public ActionResult Add()
        {
            return View();
        }
        // POST: Good/Add
        [HttpPost]
        public ActionResult Add(string data)
        {
            AppSetting setting = new AppSetting();
            var post = JObject.Parse(data);
            string ogsArea= post["ogsArea"].Value<string>();
            string ogsDesc = post["ogsDesc"].Value<string>();
            decimal ogsSalePrice = post["ogsSalePrice"].Value<decimal>();
            decimal ogsMarketPrice = post["ogsMarketPrice"].Value<decimal>();
            int ogsType = post["ogsType"].Value<int>();
            int ogsSignTime = post["ogsSignTime"].Value<int>();
            int ogsSeqNum = post["ogsSeqNum"].Value<int>();
            int ogsTimeMonth = post["ogsTimeMonth"].Value<int>();
            int time = 1;
            if (ogsType != 1) {
                time = ogsSignTime * ogsTimeMonth + ogsSeqNum;
            }
            using (var db = new qianjingEntities())
            {

                db.O_Goods.Add(new O_Goods
                {
                    ogsArea=ogsArea,
                    ogsDesc = ogsDesc,
                    ogsSalePrice = ogsSalePrice,
                    ogsMarketPrice = ogsMarketPrice,
                    ogsType = ogsType,
                    ogsSignTime = ogsSignTime,
                    ogsTime = time,
                    ogsSeqNum = ogsSeqNum,
                    ogsTimeMonth=ogsTimeMonth,
                    RowStatus = 1,
                    CreatedDate = DateTime.Now
                });
                db.SaveChanges();
            }

            return RedirectToAction("Add");

        }


        // POST: Good/Edit
        [HttpPost]
        public ActionResult Edit(string data)
        {
            AppSetting setting = new AppSetting();
            var post = JObject.Parse(data);
            int OGSID = post["OGSID"].Value<int>();
            string ogsDesc = post["ogsDesc"].Value<string>();
            decimal ogsSalePrice = post["ogsSalePrice"].Value<decimal>();
            decimal ogsMarketPrice = post["ogsMarketPrice"].Value<decimal>();
            int ogsType = post["ogsType"].Value<int>();
            int ogsSignTime = post["ogsSignTime"].Value<int>();
            int ogsTime = post["ogsTime"].Value<int>();
            int ogsSeqNum = post["ogsSeqNum"].Value<int>();
            string ogsArea = post["ogsArea"].Value<string>();

            using (var db = new qianjingEntities())
            {
                var query = db.O_Goods.FirstOrDefault(a => a.OGSID== OGSID);
                query.ogsArea = ogsArea;
                query.ogsDesc = ogsDesc;
                query.ogsSalePrice = ogsSalePrice;
                query.ogsMarketPrice = ogsMarketPrice;
                query.ogsType = ogsType;
                query.ogsSignTime = ogsSignTime;
                query.ogsTime = ogsTime;
                query.ogsSeqNum = ogsSeqNum;
                query.RowStatus = 1;
                query.CreatedDate = DateTime.Now;

                db.SaveChanges();
            }

            return RedirectToAction("Index");

        }

        // GET: Good/Edit
        public ActionResult Edit(int id)
        {
            GoodModel model = new GoodModel();
            using (var db = new qianjingEntities())
            {
                var query = db.O_Goods.FirstOrDefault(a => a.OGSID == id);
                if (query != null)
                {
                    model.OGSID = id;
                    model.ogsDesc = query.ogsDesc;
                    model.ogsSalePrice = query.ogsSalePrice;
                    model.ogsMarketPrice = query.ogsMarketPrice == null ? 0 : query.ogsMarketPrice.Value;
                    model.ogsType = query.ogsType;
                    model.ogsSignTime = query.ogsSignTime == null ? 0 : query.ogsSignTime.Value;
                    model.ogsTime = query.ogsTime;
                    model.ogsSeqNum = query.ogsSeqNum == null ? 0 : query.ogsSeqNum.Value;
                    model.ogsArea = query.ogsArea;
        
                }
            }
            return View(model);
        }


    }



}